package com.luke.dubbo.order;

import com.alibaba.dubbo.rpc.service.EchoService;
import com.luke.dubbo.api.bean.Person;
import com.luke.dubbo.api.service.CallbackListener;
import com.luke.dubbo.api.service.CallbackService;
import com.luke.dubbo.api.service.IDemoService;
import com.luke.dubbo.api.service.OrderService;
import com.luke.dubbo.order.service.impl.event.Notify;
import com.luke.dubbo.order.service.impl.event.NotifyImpl;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Consumer {

  public static void main(String[] args) throws Exception {
    ClassPathXmlApplicationContext context =
            new ClassPathXmlApplicationContext("consumer.xml");

    //回声测试
    /*EchoService echoService = (EchoService)context.getBean("userService");
    Object status = echoService.$echo("ok");*/

    /*OrderService orderService = context.getBean(OrderService.class);
    orderService.initOrder("1");*/

    //参数回调
    /*CallbackService callbackService = (CallbackService) context.getBean("callbackService");
    callbackService.addListener("foo.bar", new CallbackListener(){
      public void changed(String msg) {
        System.out.println("callback1:" + msg);
      }
    });*/

    //事件通知
    IDemoService demoService = (IDemoService)context.getBean("demoService");
    NotifyImpl notify = (NotifyImpl)context.getBean("notify");
    int requestId = 2;
    Person person = demoService.get(requestId);
    System.out.println("person:"+person);
    while (true){
      if (!notify.ret.containsKey(requestId)) {
        Thread.sleep(200);
      } else {
        break;
      }
    }
    System.out.println("result:"+notify.ret.get(requestId));

    context.start();
    System.in.read(); // 按任意键退出
  }

}
